package com.ws.shop.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.entity.Member;
import com.ws.shop.entity.Smtp;
import com.ws.shop.service.SmtpService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/smtp/*")
public class SmtpAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(SmtpAction.class);
	@Autowired
	private SmtpService smtpService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(Model model){
		List<Smtp> smtpList = smtpService.findSmtpListAll();
		model.addAttribute("smtpList", smtpList);
		model.addAttribute("stateDescs", Smtp.STATE_DESCS);
		return "smtp_index";
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id > 0){
			Smtp smtp = smtpService.loadById(id);
			if(smtp == null){
				model.addAttribute(ERROR_MSG, "编辑的邮箱信息不存在");
				return ERROR;
			}
			model.addAttribute("smtp", smtp);
		}
		return "smtp_input";
	}

	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String name = RequestUtil.getStringParameter(request, "name", null);
		String password = RequestUtil.getStringParameter(request, "password", null);
		String mail = RequestUtil.getStringParameter(request, "mail", null);
		String smtp = RequestUtil.getStringParameter(request, "smtp", null);
		int port = RequestUtil.getIntParameter(request, "port", 25);
		int auth = RequestUtil.getIntParameter(request, "auth", 1);
		int ssls = RequestUtil.getIntParameter(request, "ssls", 0);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		Smtp smtps = new Smtp(id,name,password,smtp,mail,port,auth,ssls,state);
		try{
			smtpService.saveOrUpdate(smtps);
			smtpService.reloadSmtp();
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败："+smtps,e);
			return this.ajaxJsonErrorMessage(response, "系统繁忙，请稍后再试");
		}
	}
	
	@RequestMapping(value="test.jhtml",method=RequestMethod.POST)
	public String test(HttpServletRequest request,HttpServletResponse response){
		Member member = new Member();
		member.setRealname("测试");
		member.setMail("282149042@qq.com");
		smtpService.sendResetPwdMail(member, 1, "http://localhost:8080/test.html");
		return this.ajaxJsonSuccessMessage(response, "发送成功");
	}
}
